package me.zhengjie.logging.repository

import me.zhengjie.logging.domain.SysLog
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.redis.core.RedisHash
import org.springframework.stereotype.Repository

/**
 * @author Kuki Wu
 * @date 2024-11-24
 */
@Repository
interface LogRepository : JpaRepository<SysLog?, Long?>, JpaSpecificationExecutor<SysLog?> {
    /**
     * 获取一个时间段的IP记录
     */
    @Query(
        value = "select count(*) FROM (select request_ip FROM sys_log where create_time between ?1 and ?2 GROUP BY request_ip) as s",
        nativeQuery = true
    )
    fun findIpNotNull(date1: String?, date2: String?): Long?

    @Query(value = "select l FROM SysLog l where l.id = ?1")
    fun findExceptionById(id: Long?): SysLog

    /**
     * 根据日志类型删除信息
     * @param logType 日志类型
     */
    @Modifying
    @Query(value = "delete from sys_log where log_type = ?1", nativeQuery = true)
    fun deleteByLogType(logType: String?)
}